function [Mm]=var_compn(A)
%COMPN   Create companion matrix for VAR(p)
%        b=compn(a) takes the polynomial A=[A0,A1, ..., Ap]  and creates 
%        the associated companion matrix 
%                
%             Mm= [A0 A1 A2 ...  Ap-1 Ap;
%                  1  0  0         0  0;
%                  0  1  0 ...     0  0;
%                  :               :  :;
%                  0  0  0 ...     1  0];              
%            y(t)= Mm * y(t-1) + e(t)
K=size(A,1);
p=size(A,2)/K; %number of lags

Mm=zeros(K*p,K*p);
Mm(1:K,:)=A;
Mm(K+1:K*p,1:(p-1)*K)=eye(K*(p-1));